package com.aerotechnic.dao.implementation;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.RessourceTrackDaoIn;
import com.aerotechnic.entities.RessourceTrack;

@Repository
public class RessourceTrackDao implements RessourceTrackDaoIn {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(RessourceTrack ressourceTrack) {
		// TODO Auto-generated method stub
		getCurrentSession().save(ressourceTrack);

	}

	@Override
	public void update(RessourceTrack ressourceTrack) {
		// TODO Auto-generated method stub
		getCurrentSession().update(ressourceTrack);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(findByID(id));
	}

	@Override
	public RessourceTrack findByID(Long id) {
		// TODO Auto-generated method stub
		return (RessourceTrack) getCurrentSession()
				.createQuery("from RessourceTrack where id = " + id).list()
				.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RessourceTrack> findAll() {
		// TODO Auto-generated method stub
		return (List<RessourceTrack>) getCurrentSession().createQuery(
				"from RessourceTrack ").list();
	}

}
